package meterMonitor.controller;

import meterMonitor.biz.UserAuthBiz;
import meterMonitor.biz.UserBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.SysUsersPO;
import meterMonitor.service.SysUsersService;
import meterMonitor.vo.BaseResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2019/2/23 0023.
 * 创建用户
 */
@RequestMapping("userManger")
@RestController
public class CreateUserController extends BaseController {


    @Autowired
    UserAuthBiz userAuthBiz;
    @Autowired
    UserBiz userBiz;

    @Autowired
    SysUsersService sysUsersService;

    /**
     * 用户管理页面
     *
     * @param mv
     * @return
     */
    @RequestMapping("userManger.do")
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    /**
     * 查询所有用户以及用户所对应的权限
     */
    @RequestMapping("getAll.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public BaseResponseData getAll() {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            List<SysUsersPO> sysUsers = sysUsersService.queryList(new SysUsersPO());
            baseResponseData.setData(sysUsers);
            baseResponseData.setCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("查询用户信息失败！");
        }
        return baseResponseData;
    }


    /**
     * 添加用户
     * 向用户表添加记录
     * 向用户权限表添加记录
     *
     * @param
     * @return currentUser
     */
    @RequestMapping("insertUser.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public BaseResponseData insertUser(SysUsersPO sysUser) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {

            SysUsersPO userByName = sysUsersService.getUserByName(sysUser.getUserName());
            if (Objects.nonNull(userByName)) {
                baseResponseData.setMessage("该用户名已经被注册，请重新输入！");
                baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            } else {
                sysUsersService.addSysUsers(sysUser);
                baseResponseData.setMessage("添加用户成功！");
                baseResponseData.setCode(ResponseCode.SUCCESS);
            }
        } catch (Exception e) {
            logger.error("添加用户失败", e);
            baseResponseData.setMessage("添加用户失败");
        }
        return baseResponseData;
    }


    /**
     * 删除用户
     */
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    @RequestMapping("deleteUser.do")
    @ResponseBody
    public BaseResponseData deleteUser(SysUsersPO user) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            sysUsersService.deleteSysUsersById(user.getUserId());
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("删除用户成功！");
            return baseResponseData;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("删除用户失败！");
        }
        return baseResponseData;
    }


    /**
     * 编辑用户
     */
    @RequestMapping("editUser.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public BaseResponseData editUser(SysUsersPO sysUsers) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            sysUsersService.updateSysUsers(sysUsers);
            baseResponseData.setMessage("编辑用户成功！");
            baseResponseData.setCode(ResponseCode.SUCCESS);
            return baseResponseData;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("编辑用户失败！");
        }
        return baseResponseData;
    }


    // /**
    //  * 查询用户的所有权限
    //  *
    //  * @return
    //  */
    // @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    // @RequestMapping("getAuth.do")
    // @ResponseBody
    // public List<Auth> selectAuth() {
    //     try {
    //         return userAuthBiz.getAuths();
    //     } catch (Exception e) {
    //         this.logger.error(e.getMessage(), e);
    //     }
    //
    //     return null;
    // }


}
